var roleId_static;
$(function(){
  $.ajaxSetup({
    type: 'POST',
    complete: function(xhr,status) {
      var sessionStatus = xhr.getResponseHeader('sessionstatus');
      if(sessionStatus == 'timeout') {
        window.location.href = 'login.html';
      }
    }
  });
  initLoginInfo();
  initLoginRoleInfo();
  loginout();

  updatePass();

  if(roleId_static == 7){
    $(".layui-header").html(getPzHeadHtml());
  }else{
    $(".layui-header").html(getHeadHtml());
  }
  //副总和超级管理员可以查看所有员工业绩
  if(roleId_static == 3 || roleId_static == 6 || roleId_static == -1 || roleId_static == 4){
    $("#profitUserTableDivParent").css("display","");
    $("#contentIdDiv").css("margin-top","330px");
  }

  $(".layui-header").on("click",".signJump1",function(){
    var dept = $("#userDept").val();
    var loginUserRoleId = $("#loginUserRoleId").val();
    if(checkDept(dept) == 1 || dept == 0 || loginUserRoleId == -1 || loginUserRoleId == 8 || loginUserRoleId == 4){
      window.location.href = "signcustommanage.html";
    }else{
      toastrComponent.show("warning", "对不起你没有权限！");
    }

  });

  $(".layui-header").on("click",".signJump2",function(){
    var dept = $("#userDept").val();
    var loginUserRoleId = $("#loginUserRoleId").val();
    if(checkDept(dept) == 2 || dept == 0 || loginUserRoleId == -1 || loginUserRoleId == 8 || loginUserRoleId == 4){
      window.location.href = "signqycustommanage.html";
    }else{
      toastrComponent.show("warning", "对不起你没有权限！");
    }
  });

  $(".layui-header").on("click",".applyJump1",function(){
    var dept = $("#userDept").val();
    var loginUserRoleId = $("#loginUserRoleId").val();
    if(checkDept(dept) == 1 || dept == 0 || loginUserRoleId == -1 || loginUserRoleId == 8 || loginUserRoleId == 4){
      window.location.href = "papply.html";
    }else{
      toastrComponent.show("warning", "对不起你没有权限！");
    }

  });

  $(".layui-header").on("click",".applyJump2",function(){
    var dept = $("#userDept").val();
    var loginUserRoleId = $("#loginUserRoleId").val();
    if(checkDept(dept) == 2 || dept == 0 || loginUserRoleId == -1 || loginUserRoleId == 8 || loginUserRoleId == 4){
      window.location.href = "tapply.html";
    }else{
      toastrComponent.show("warning", "对不起你没有权限！");
    }
  });

  $(".layui-header").on("click",".userJump1",function(){
    var loginUserRoleId = $("#loginUserRoleId").val();
    if(loginUserRoleId == -1){
      window.location.href = "userManage.html";
    }else{
      toastrComponent.show("warning", "对不起你没有权限！");
    }
  });

  $("#indexLogo").on("click",function(){
    window.location.href = "zlmanage.html";
  });

  $("#signMatchJump").on("click",function(){
    $.ajax({
      type : "get",
      url : "getUserMatchIds.do",
      success : function(data) {
        if(data == "success"){
          window.location.href = "signcustomermatch.html";
        }else {
          toastrComponent.show("warning", "对不起你没有权限！");
        }
      }
    });
  });

  $(".kitJump").on("click",function(){
    window.location.href = "systemfile.html";
  });

});

function checkDept(deptNum) {
  if(deptNum == 0){
    return 0;
  }
  if(deptNum == 1 || deptNum == 3 || deptNum == 99){
    return 1;
  }
  if(deptNum == 2 || deptNum == 4 || deptNum == 100){
    return 2;
  }
}

function initLoginRoleInfo() {
  $.ajax({
    type : "get",
    async : false,
    url : "getLoginInfo.do",
    success : function(v) {
      roleId_static = v.roleId;
    }
  });

}

function initLoginInfo() {
    $.ajax({
      type : "get",
      url : "getLoginInfo.do",
      success : function(v) {
        $("#userName").text(v.userId);
        $("#loginUserId").val(v.userId);
        $("#loginUserRoleId").val(v.roleId);
        $("#userId").val(v.id);
        $("#userDept").val(v.userDept);
      }
    });

}

function loginout() {
  $(".layui-header").on("click","#loginout",function () {
    $.ajax({
      type : "get",
      url : "loginout.do",
      success : function(v) {
        if (v == 'success') {
          window.location.href = 'login.html';
        }
      }
    });
  });
}
function updatePass() {
  $(".layui-header").on("click","#updatePass",function () {
    window.location.href = 'passupdate.html';
  });
}

function getPzHeadHtml() {
  var html = '<div id="indexLogo" class="layui-logo" style="cursor: pointer;">菁霖商务信息咨询有限公司</div>'
      +'<ul class="layui-nav layui-layout-left" class="text-center">'
      +'<li class="layui-nav-item">'
      +'<a href="javascript:;">签约客户信息</a>'
      +'<dl class="layui-nav-child">'
      +'<dd><a class="signJump1" href="javascript:;">人才部签约客户</a></dd>'
      +'<dd><a class="signJump2" href="javascript:;">企业部签约客户</a></dd>'
      +'</dl>'
      +'</li>'
      +'<li class="layui-nav-item"><a id="signMatchJump" href="javascript:;">&nbsp;客户匹配&nbsp;</a></li>'
      +'</ul>'
      +'<ul class="layui-nav layui-layout-right">'
      +'<li class="layui-nav-item">'
      +'<a href="javascript:;">'
      +'<img src="./static/images/logo.jpg" class="layui-nav-img">'
      +'<span id="userName"></span>'
      +'<input type="hidden" value="" id="loginUserId">'
      +'<input type="hidden" value="" id="loginUserRoleId">'
      +'<input type="hidden" value="" id="userId">'
      +'<input type="hidden" value="" id="userDept">'
      +'</a>'
      +'<dl class="layui-nav-child">'
      +'<dd><a href="javascript:;" id="loginout">退出</a></dd>'
      +'<dd><a href="javascript:;" id="updatePass">修改密码</a></dd>'
      +'</dl>'
      +'</li>'
      +'</ul>';
  return html;
}

function getHeadHtml() {
  var html = '<div id="indexLogo" class="layui-logo" style="cursor: pointer;">菁霖商务信息咨询有限公司</div>'
            +'<ul class="layui-nav layui-layout-left" class="text-center">'
            +'<li class="layui-nav-item">'
            +'<a href="resourcemanage.html">资源信息</a>'
            +'<!--<dl class="layui-nav-child">-->'
            +'<!--<dd><a href="resourcemanage.html">人才部资源信息</a></dd>-->'
            +'<!--<dd><a href="resourcemanage.html">企业部资源信息</a></dd>-->'
            +'<!--</dl>-->'
            +'</li>'
            +'<li class="layui-nav-item">'
            +'<a href="javascript:;">签约客户信息</a>'
            +'<dl class="layui-nav-child">'
            +'<dd><a class="signJump1" href="javascript:;">人才部签约客户</a></dd>'
            +'<dd><a class="signJump2" href="javascript:;">企业部签约客户</a></dd>'
            +'</dl>'
            +'</li>'
            +'<li class="layui-nav-item">'
            +'<a href="javascript:;">转款/回款</a>'
            +'<dl class="layui-nav-child">'
            +'<dd><a class="applyJump1" href="javascript:;">转款审批</a></dd>'
            +'<dd><a class="applyJump2" href="javascript:;">回款审批</a></dd>'
            +'</dl>'
            +'</li>'
            +'<li class="layui-nav-item">'
            +'<a href="javascript:;">费用报销</a>'
            +'<dl class="layui-nav-child">'
            +'<dd><a class="" href="expendapproval.html">已签约报销</a></dd>'
            +'<dd><a class="" href="expendlist.html">未签约报销</a></dd>'
            +'</dl>'
            +'</li>'
            +'<li class="layui-nav-item"><a id="signMatchJump" href="javascript:;">&nbsp;客户匹配&nbsp;</a></li>'
            +'<li class="layui-nav-item">'
            +'<a href="javascript:;">系统管理</a>'
            +'<dl class="layui-nav-child">'
            +'<dd><a class="userJump1" href="javascript:;">用户管理</a></dd>'
            +'<dd><a class="kitJump" href="javascript:;">公司资料</a></dd>'
            +'</dl>'
            +'</li>'
            +'</ul>'
            +'<ul class="layui-nav layui-layout-right">'
            +'<li class="layui-nav-item">'
            +'<a href="javascript:;">'
            +'<img src="./static/images/logo.jpg" class="layui-nav-img">'
            +'<span id="userName"></span>'
            +'<input type="hidden" value="" id="loginUserId">'
            +'<input type="hidden" value="" id="loginUserRoleId">'
            +'<input type="hidden" value="" id="userId">'
            +'<input type="hidden" value="" id="userDept">'
            +'</a>'
            +'<dl class="layui-nav-child">'
            +'<dd><a href="javascript:;" id="loginout">退出</a></dd>'
            +'<dd><a href="javascript:;" id="updatePass">修改密码</a></dd>'
            +'</dl>'
            +'</li>'
            +'</ul>';
  return html;
}

function getKitHtml(data) {
  var html = "";
  var userId = $("#userId").val();
  var loginUserRoleId = $("#loginUserRoleId").val();
  $.each(data,function (index,item) {
    html += '<li><a style="cursor: pointer;text-decoration:underline;" class="kitUrl" id='+item.id+'>'+item.fileName+'</a>';
    if(userId == item.uploadUserId || loginUserRoleId == -1){
      html += '&nbsp;&nbsp;&nbsp;<a style="cursor: pointer;color: red;" class="deleteKit" id='+item.id+'>删除</a></li>';
    }
  })
  return html;
}
